package com.chixing.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.chixing.entity.Mycoupon;
import com.chixing.entity.VO.myCouponDetailVO;
import com.chixing.mapper.MycouponMapper;
import com.chixing.service.IMycouponService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chixing.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author smith
 * @since 2025-09-22
 */
@Service
public class MycouponServiceImpl  implements IMycouponService {

    @Autowired
    private MycouponMapper mycouponMapper;

    @Override
    public Result getById(Integer myCouponId) {
        Mycoupon mycoupon = mycouponMapper.selectById(myCouponId);
        if (mycoupon!=null)
            return Result.getSuccess(mycoupon);
        return Result.getFail(null);
    }

    @Override
    public Result save(Mycoupon mycoupon) {
        // 我的优惠券编号, 用于核销业务
        String mycouponNumber = UUID.randomUUID().toString().replace("-","");
        mycoupon.setMycouponNumber(mycouponNumber);
        mycoupon.setCreateTime(LocalDateTime.now());
        mycoupon.setMycouponStatus(0);// 0: 有效（未使用）  1： 已使用 2： 已过期

        int rows = mycouponMapper.insert(mycoupon);
        if (rows>0)
            return Result.saveSuccess(mycoupon);
        return Result.saveFail(null);
    }

    @Override
    public Result getMyAllCoupons(Integer userId, Integer mycouponStatus) {
        List<myCouponDetailVO> myCouponDetailVOS = mycouponMapper.selectAllCoupons(userId, mycouponStatus);
        if (myCouponDetailVOS!=null && myCouponDetailVOS.size()>0)
            return Result.getSuccess(myCouponDetailVOS);
        return Result.getFail(null);
    }

    @Override
    public Result updateStatus(Integer myCouponId, Integer myCouponStatus) {
        UpdateWrapper<Mycoupon> wrapper = new UpdateWrapper<>();
        wrapper.eq("mycou_id",myCouponId).set("mycoupon_status",myCouponStatus);
        int rows = mycouponMapper.update(wrapper);
        if (rows>0)
            return Result.updateSuccess(myCouponId);
        return Result.updateFail(null);
    }
}
